﻿using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using TSharp.Core.Mvc.Pager;
using TSharp.Core.Util;

namespace TSharp.Core.Authentication.Facade
{
    /// <summary>
    /// 用户服务
    /// <para>by tangjingbo at 2010/7/5 23:13</para>
    /// </summary>
    public interface IMembershipService
    {
        /// <summary>
        /// 获取锁定成员资格用户前允许的无效密码或无效密码提示问题答案尝试次数。
        /// </summary>
        /// <value>The max invalid password attempts.</value>
        int MaxInvalidPasswordAttempts { get; }

        /// <summary>
        /// 最小密码长度
        /// <para>by tangjingbo at 2010/7/5 23:14</para>
        /// </summary>
        /// <value></value>
        int MinPasswordLength { get; }

        #region 密码验证，修改

        /// <summary>
        /// 验证用户密码
        /// <para>by tangjingbo at 2010/7/5 23:14</para>
        /// </summary>
        /// <param name="context">The context.</param>
        /// <param name="key">Name of the user.</param>
        /// <param name="password">The password.</param>
        /// <returns></returns>
        bool ValidateUser(HttpContextBase context, string key, string password);

        /// <summary>
        /// 修改当前用户密码
        /// <para>by tangjingbo at 2010/7/5 23:15</para>
        /// </summary>
        /// <param name="key">Name of the user.</param>
        /// <param name="oldPassword">The old password.</param>
        /// <param name="newPassword">The new password.</param>
        /// <returns></returns>
        bool ChangePassword(string key, string oldPassword, string newPassword);

        /// <summary>
        /// 根据找回密码token重置密码
        /// </summary>
        /// <param name="url">The URL.</param>
        /// <param name="newPassword">The new password.</param>
        /// <returns></returns>
        bool ResetPassword(ParameterizedUri url, string newPassword);

        /// <summary>
        /// 生成一个针对用户名的有时效的签名
        /// <para>by tangjingbo at 2010/7/6 22:18</para>
        /// </summary>
        /// <param name="userName">Name of the user.</param>
        /// <returns></returns>
        ParameterizedUri GetResetPasswordUrl(string userName);


        /// <summary>
        /// 验证一个用户名的签名url是否有效
        /// </summary>
        /// <param name="url">The URL.</param>
        /// <returns></returns>
        bool ValidateResetPasswordUrl(ParameterizedUri url);

        #endregion

        #region 创建用户

        /// <summary>
        /// 创建用户
        /// <para>by tangjingbo at 2010/7/5 23:14</para>
        /// </summary>
        /// <param name="key">Name of the user.</param>
        /// <param name="password">The password.</param>
        /// <param name="email">The email.</param>
        /// <returns></returns>
        MembershipCreateStatus CreateUser(string key, string password, string email);

        /// <summary>
        /// Creates the user.
        /// </summary>
        /// <param name="key">Name of the user.</param>
        /// <param name="password">The password.</param>
        /// <param name="email">The email.</param>
        /// <param name="passwordQuestion">The password question.</param>
        /// <param name="passwordAnswer">The password answer.</param>
        /// <param name="isApproved">if set to <c>true</c> [is approved].</param>
        /// <param name="providerUserKey">The provider user key.</param>
        /// <returns></returns>
        MembershipCreateStatus CreateUser(string key,
                                          string password,
                                          string email,
                                          string passwordQuestion,
                                          string passwordAnswer,
                                          bool isApproved,
                                          object providerUserKey
            );

        #endregion

        #region 获取用户

        /// <summary>
        /// Gets the user.
        /// </summary>
        /// <param name="context">The context.</param>
        /// <param name="username">The username.</param>
        /// <param name="userIsOnline">if set to <c>true</c> [user is online].</param>
        /// <returns></returns>
        IUser GetUser(HttpContextBase context, string username, bool userIsOnline);

        /// <summary>
        /// Gets the specified context.
        /// </summary>
        /// <param name="context">The context.</param>
        /// <param name="providerUserKey">The provider user key.</param>
        /// <returns></returns>
        IUser Get(HttpContextBase context, object providerUserKey);

        /// <summary>
        /// 获取用户列表
        /// </summary>
        /// <param name="context">The context.</param>
        /// <param name="pageIndex">Index of the page.</param>
        /// <param name="pageSize">Size of the page.</param>
        /// <param name="totalRecords">The total records.</param>
        /// <returns></returns>
        List<IUser> GetUsers(HttpContextBase context, int pageIndex, int pageSize, out int totalRecords);

        /// <summary>
        /// Finds all.
        /// </summary>
        /// <param name="context">The context.</param>
        /// <param name="pageIndex">Index of the page.</param>
        /// <param name="pageSize">Size of the page.</param>
        /// <returns></returns>
        PagedList<IUser> FindAll(HttpContextBase context, int pageIndex, int pageSize);

        /// <summary>
        /// Finds the by email.
        /// </summary>
        /// <param name="context">The context.</param>
        /// <param name="emailAddressToMatch">The email address to match.</param>
        /// <param name="pageIndex">Index of the page.</param>
        /// <param name="pageSize">Size of the page.</param>
        /// <returns></returns>
        PagedList<IUser> FindByEmail(HttpContextBase context, string emailAddressToMatch, int pageIndex, int pageSize);

        /// <summary>
        /// Finds the name of the by user.
        /// </summary>
        /// <param name="context">The context.</param>
        /// <param name="userNameToMatch">The user name to match.</param>
        /// <param name="pageIndex">Index of the page.</param>
        /// <param name="pageSize">Size of the page.</param>
        /// <returns></returns>
        PagedList<IUser> FindByUserName(HttpContextBase context, string userNameToMatch, int pageIndex, int pageSize);

        /// <summary>
        /// Touches the specified context.
        /// </summary>
        /// <param name="context">The context.</param>
        /// <param name="user">The user.</param>
        /// <returns></returns>
        IUser Touch(HttpContextBase context, IUser user);

        /// <summary>
        /// Touches the specified context.
        /// </summary>
        /// <param name="context">The context.</param>
        /// <param name="key">Name of the user.</param>
        /// <returns></returns>
        IUser Touch(HttpContextBase context, string key);

        /// <summary>
        /// Touches the specified context.
        /// </summary>
        /// <param name="context">The context.</param>
        /// <param name="providerUserKey">The provider user key.</param>
        /// <returns></returns>
        IUser Touch(HttpContextBase context, object providerUserKey);

        #endregion

        /// <summary>
        /// 获取在线人数
        /// </summary>
        /// <returns></returns>
        int GetNumberOfUsersOnline();

// int UsersOnline { get; }
        /// <summary>
        /// Gets the total users.
        /// </summary>
        /// <returns></returns>
        int GetTotalUsers();

        /// <summary>
        /// 删除用户
        /// </summary>
        /// <param name="context">The context.</param>
        /// <param name="key">Name of the user.</param>
        /// <returns></returns>
        bool DeleteUser(HttpContextBase context, string key);

        /// <summary>
        /// 删除用户
        /// </summary>
        /// <param name="context">The context.</param>
        /// <param name="user">用户实例</param>
        /// <returns></returns>
        bool DeleteUser(HttpContextBase context, IUser user);

        /// <summary>
        /// Deletes the specified context.
        /// </summary>
        /// <param name="context">The context.</param>
        /// <param name="user">The user.</param>
        /// <param name="deleteAllRelatedData">if set to <c>true</c> [delete all related data].</param>
        void Delete(HttpContextBase context, IUser user, bool deleteAllRelatedData);

        /// <summary>
        /// 更新用户信息
        /// </summary>
        /// <param name="context">The context.</param>
        /// <param name="user">需要更新的用户实例</param>
        /// <returns></returns>
        bool Update(HttpContextBase context, IUser user);

        /// <summary>
        /// Unlock the user.
        /// </summary>
        /// <param name="username">The username.</param>
        /// <returns></returns>
        bool UnLockUser(string username);
    }
}